package ex55.mm.flow.impound.quote.common.kit;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import ex55.mm.flow.impound.quote.exchange.api.ExchangeException;

import java.math.BigDecimal;

public class JsonWrapperArray {

    private JSONArray array = null;

    public JsonWrapperArray(JSONArray array) {
        this.array = array;
    }

    public JSONArray getArray() {
        return array;
    }

    public JsonWrapper getJsonObjectAt(int index) throws ExchangeException {
        if (array != null && array.size() > index) {
            JSONObject object = (JSONObject) array.get(index);
            if (object == null) {
                throw ExceptionKit.handler(new IllegalArgumentException("[Json] Cannot get array at index " + index + " in array"));
            }
            return new JsonWrapper(object);
        } else {
            throw ExceptionKit.handler(new IllegalArgumentException("[Json] Index is out of bound or array is null"));
        }
    }

    public static boolean isEmpty(JsonWrapperArray jsonWrapperArray) {
        return jsonWrapperArray == null || jsonWrapperArray.getArray() == null || jsonWrapperArray.getArray().size() == 0;
    }

    public JsonWrapperArray getArrayAt(int index) throws ExchangeException {
        if (array != null && array.size() > index) {
            JSONArray newArray = (JSONArray) array.get(index);
            if (newArray == null) {
                throw ExceptionKit.handler(new IllegalArgumentException("[Json] Cannot get array at index " + index + " in array"));
            }
            return new JsonWrapperArray(newArray);
        } else {
            throw ExceptionKit.handler(new IllegalArgumentException("[Json] Index is out of bound or array is null"));
        }
    }

    private Object getObjectAt(int index) throws ExchangeException {
        if (array != null && array.size() > index) {
            return array.get(index);
        } else {
            throw ExceptionKit.handler(new IllegalArgumentException("[Json] Index is out of bound or array is null"));
        }
    }

    public long getLongAt(int index) throws ExchangeException {
        try {
            return (Long) getObjectAt(index);
        } catch (Exception e) {
            throw ExceptionKit.handler(new IllegalArgumentException("[Json] Cannot get long at index " + index + " in array: " + e.getMessage()));
        }
    }

    public int getIntegerAt(int index) throws ExchangeException {
        try {
            return (Integer) getObjectAt(index);
        } catch (Exception e) {
            throw ExceptionKit.handler(new IllegalArgumentException("[Json] Cannot get int at index " + index + " in array: " + e.getMessage()));
        }
    }

    public BigDecimal getBigDecimalAt(int index) throws ExchangeException {

        try {
            Object objectAt = getObjectAt(index);
            if (objectAt instanceof Integer || objectAt instanceof Long) {
                return new BigDecimal(((Integer) getObjectAt(index)));
            }

            return new BigDecimal(((BigDecimal) getObjectAt(index)).stripTrailingZeros().toPlainString());
        } catch (Exception e) {
            throw ExceptionKit.handler(e);
        }
    }

    public String getStringAt(int index) throws ExchangeException {
        try {
            return (String) getObjectAt(index);
        } catch (Exception e) {
            throw ExceptionKit.handler(e);
        }
    }

}

